﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Logic.Packet;
using System.Runtime.Remoting.Metadata.W3cXsd2001;

namespace Logic.Packet.Factories
{
    public class PakClientFactory : IPakFactory
    {
        public PakClientFactory()
        {
        }
        public T4CBasePak Parse(byte[] data)
        {
            string datahex = new SoapHexBinary(data).ToString();
            T4CBasePak basicPak = new T4CBasePak(data);
            Type to = GetTypeToCreate(basicPak);
            try
            {
                return Activator.CreateInstance(to, basicPak) as T4CBasePak;
            }
            catch (Exception)
            {

                return Activator.CreateInstance(typeof(NotImplementedPak), basicPak) as T4CBasePak;
            }

        }

        Type GetTypeToCreate(T4CBasePak basePak)
        {
            if (Packet.Paks.ClientPaks.ContainsKey(basePak.ActionID))
                return Packet.Paks.ClientPaks[basePak.ActionID];
            else
                return typeof(NotImplementedPak);

        }
    }
}
